package clientView;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;

import object.TimeSheet;
import utility.StringUtil;
import clientMain.Main;
import clientUtilGUI.UtilityGUI;

public class TS_View extends JPanel{
	
	private JPanel panelHeader;
	private JSplitPane splitPane;
	
	private JPanel panelLeft;
	private GridBagLayout gbl_panelLeft;
	private JLabel lblDepartment;
	private GridBagConstraints gbc_lblDepartment;
	private JLabel lblDepName;
	private JTable tblListEmDep;
	private JScrollPane scrolltblList;
	private GridBagConstraints gbc_scrolltblList;
	
	private JPanel panelRight;
	private GridBagLayout gbl_panelRight;
	private JLabel lblEmId;
	private GridBagConstraints gbc_lblEmId;
	private JLabel lblId;
	private GridBagConstraints gbc_lblId;
	private JLabel lblEmName;
	private GridBagConstraints gbc_lblEmName;
	private Container lblName;
	private GridBagConstraints gbc_lblName;
	public static JTable tblEmTs;
	private JScrollPane scrollTblTs;
	private GridBagConstraints gbc_scrollTblTs;
	
	//	Truyen vao	
	private String monthYear;
	private int depId;
	private String depName;
	//	Lay du lieu tu bang tblListEmTs
	private String emId;
	private String emFullName;
	
    public TS_View(String my, int id, String name){
    	monthYear = my;
    	depId = id;
    	depName = name;

    	setLayout(new BorderLayout(0, 0));
		
		panelHeader = UtilityGUI.headerForm("Bảng Chấm Công Tháng : "+monthYear);
		add(panelHeader, BorderLayout.NORTH);
		
		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
	    splitPane.setContinuousLayout(true);
	    splitPane.setOneTouchExpandable(true);	    	    
	    splitPane.setDividerLocation(430);
	    
	    /*--------------- Bang NV trong phong -----------------*/
	    panelLeft = new JPanel();
	    gbl_panelLeft = new GridBagLayout();
	    gbl_panelLeft.columnWidths = new int[]{0, 0};
	    gbl_panelLeft.rowHeights = new int[]{0, 0};
	    gbl_panelLeft.columnWeights = new double[]{0.0, 1.0};
	    gbl_panelLeft.rowWeights = new double[]{0.0, 1.0};
	    panelLeft.setLayout(gbl_panelLeft);
	    
	    //	Title	    
	    lblDepartment = new JLabel("Phòng :");
	    lblDepartment.setFont(UtilityGUI.fontHeader2);
	    gbc_lblDepartment = new GridBagConstraints();
	    gbc_lblDepartment.anchor = GridBagConstraints.WEST;
	    gbc_lblDepartment.insets = new Insets(5, 15, 5, 5);
	    gbc_lblDepartment.gridx = 0;
	    gbc_lblDepartment.gridy = 0;
	    panelLeft.add(lblDepartment, gbc_lblDepartment);
	    
	    //	Table
	    tblListEmDep = UtilityGUI.createTblListEmDep(depId, true, true, 1, null, true);
	    tblListEmDep.setCursor(UtilityGUI.cursorBtn);
	    //	Add Mouse Event	    
	    tblListEmDep.addMouseListener(new MouseListEm());
	    
	    lblDepName = new JLabel(depName);
	    lblDepName.setFont(UtilityGUI.fontHeader2);
	    lblDepName.setForeground(UtilityGUI.colorHeader2);
	    GridBagConstraints gbc_lblDepName = new GridBagConstraints();
	    gbc_lblDepName.anchor = GridBagConstraints.WEST;
	    gbc_lblDepName.insets = new Insets(5, 0, 5, 8);
	    gbc_lblDepName.gridx = 1;
	    gbc_lblDepName.gridy = 0;
	    panelLeft.add(lblDepName, gbc_lblDepName);
	    
	    scrolltblList = new JScrollPane();
	    scrolltblList.setViewportView(tblListEmDep);
	    gbc_scrolltblList = new GridBagConstraints();
	    gbc_scrolltblList.insets = new Insets(0, 15, 0, 5);
	    gbc_scrolltblList.fill = GridBagConstraints.BOTH;
	    gbc_scrolltblList.gridx = 0;
	    gbc_scrolltblList.gridy = 1;
	    gbc_scrolltblList.gridwidth = 2;
	    panelLeft.add(scrolltblList, gbc_scrolltblList);
	    splitPane.setLeftComponent(panelLeft);
	    /*-------------- END Bang NV trong phong --------------*/
	    
	    /*--------------- Bang TS cua tung NV -----------------*/
	    panelRight = new JPanel();
	    gbl_panelRight = new GridBagLayout();
	    gbl_panelRight.columnWidths = new int[]{0, 0, 0, 0};
	    gbl_panelRight.rowHeights = new int[]{0, 0};
	    gbl_panelRight.columnWeights = new double[]{0.0, 0.5, 0.0, 0.5};
	    gbl_panelRight.rowWeights = new double[]{0.0, 1.0};
	    panelRight.setLayout(gbl_panelRight);
	    
	    lblEmId = new JLabel("Mã Nhân Viên :");
	    lblEmId.setFont(UtilityGUI.fontHeader2);
	    gbc_lblEmId = new GridBagConstraints();
	    gbc_lblEmId.anchor = GridBagConstraints.WEST;
	    gbc_lblEmId.insets = new Insets(5, 15, 5, 5);
	    gbc_lblEmId.gridx = 0;
	    gbc_lblEmId.gridy = 0;
	    panelRight.add(lblEmId, gbc_lblEmId);
	    
	    lblId = new JLabel();
	    gbc_lblId = new GridBagConstraints();
	    gbc_lblId.insets = new Insets(5, 0, 5, 15);
	    gbc_lblId.anchor = GridBagConstraints.WEST;
	    gbc_lblId.gridx = 1;
	    gbc_lblId.gridy = 0;
	    panelRight.add(lblId, gbc_lblId);
	    
	    lblEmName = new JLabel("Họ và Tên :");
	    lblEmName.setFont(UtilityGUI.fontHeader2);
	    gbc_lblEmName = new GridBagConstraints();
	    gbc_lblEmName.anchor = GridBagConstraints.EAST;
	    gbc_lblEmName.insets = new Insets(5, 5, 5, 5);
	    gbc_lblEmName.gridx = 2;
	    gbc_lblEmName.gridy = 0;
	    panelRight.add(lblEmName, gbc_lblEmName);
	    
	    lblName = new JLabel();
	    gbc_lblName = new GridBagConstraints();
	    gbc_lblName.insets = new Insets(5, 0, 5, 10);
	    gbc_lblName.anchor = GridBagConstraints.WEST;
	    gbc_lblName.gridx = 3;
	    gbc_lblName.gridy = 0;
	    panelRight.add(lblName, gbc_lblName);
	     	    
	    scrollTblTs = new JScrollPane();
	    gbc_scrollTblTs = new GridBagConstraints();
		gbc_scrollTblTs.insets = new Insets(0, 15, 0, 10);
		gbc_scrollTblTs.fill = GridBagConstraints.BOTH;
		gbc_scrollTblTs.gridwidth = 4;
		gbc_scrollTblTs.gridx = 0;
		gbc_scrollTblTs.gridy = 1;
	    panelRight.add(scrollTblTs, gbc_scrollTblTs);
	    splitPane.setRightComponent(panelRight);        
	    /*--------------- Bang TS cua tung NV -----------------*/
	    
		this.add(splitPane, BorderLayout.CENTER);
		
	}
	
	private class MouseListEm extends MouseAdapter{
		 public void mouseClicked(MouseEvent e) {
			 if((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0){
				int row = tblListEmDep.getSelectedRow();
				emId = (String) tblListEmDep.getModel().getValueAt(row, 0);
				emFullName = (String) tblListEmDep.getModel().getValueAt(row, 1);
				lblId.setVisible(false);
				lblName.setVisible(false);
				lblId = new JLabel(emId);
				lblId.setFont(UtilityGUI.fontHeader2);
				lblId.setForeground(UtilityGUI.colorHeader2);
				panelRight.add(lblId, gbc_lblId);
				lblName = new JLabel(emFullName);
				lblName.setFont(UtilityGUI.fontHeader2);
				lblName.setForeground(UtilityGUI.colorHeader2);
				panelRight.add(lblName, gbc_lblName);
				tblEmTs = UtilityGUI.createTblEmTs(emId, monthYear);
				if(tblEmTs == null){
					JOptionPane.showMessageDialog(null, "Ko có dữ liệu!!!", 
						 UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
					return;
				}
				tblEmTs.setCursor(UtilityGUI.cursorBtn);
				//	Add Mouse Event
				tblEmTs.addMouseListener(new MouseEmTs());
				scrollTblTs.setVisible(false);
				scrollTblTs = new JScrollPane();
				scrollTblTs.setViewportView(tblEmTs);
				panelRight.add(scrollTblTs, gbc_scrollTblTs);
			 }
		 }
	}
	
	private class MouseEmTs extends MouseAdapter{
		public void mouseClicked(MouseEvent e) {
			if((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0){
				int row = tblEmTs.getSelectedRow();
				int col = tblEmTs.getSelectedColumn();
				if(col == 4){
					if(tblEmTs.getModel().getValueAt(row, col).equals("Thêm")){
						TimeSheet ts = new TimeSheet();
						ts.setEmId(emId);
						ts.setDate(Main.strDate);
						ts.setMor((Boolean) tblEmTs.getModel().getValueAt(row, 1));
						ts.setAft((Boolean) tblEmTs.getModel().getValueAt(row, 2));
						ts.setEven((Boolean) tblEmTs.getModel().getValueAt(row, 3));
						boolean flag = Main.actionTS.insertTs(ts, Main.userId);
						if(flag == true){
					    	JOptionPane.showMessageDialog(null, UtilityGUI.successAction, 
					    				UtilityGUI.titleAlert, JOptionPane.INFORMATION_MESSAGE);
					    	tblEmTs.getModel().setValueAt("Sửa", row, col);
						}
					}else if(tblEmTs.getModel().getValueAt(row, col).equals("Sửa")){
						Vector<String> my = StringUtil.subStr(monthYear, "/");
						String m = my.get(0);
						String y = my.get(1);
					    int month = Integer.parseInt(m);
					    int year = Integer.parseInt(y);
					    if(year == Main.fullday.getYear() + 1900){
						    if((month == Main.fullday.getMonth() && Main.fullday.getDate()<=5)
						    		|| (month == Main.fullday.getMonth()+1 && row+1 <= Main.fullday.getDate())){
						    	final String date = (String) tblEmTs.getModel().getValueAt(row, 0);
						    	new TS_Update(Main.main, date, emId, emFullName).setVisible(true);
						    }else if(month == Main.fullday.getMonth()+1 && row+1 > Main.fullday.getDate()){
						    	JOptionPane.showMessageDialog(null, "Chưa đến ngày !!!", 
						    			UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
						    }else{
						    	JOptionPane.showMessageDialog(null, "Chỉ có thể sửa thông tin muộn nhất trước ngày mùng 5 hàng tháng!", 
						    			UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
						    }
					    }else{
					    	JOptionPane.showMessageDialog(null, "Chỉ có thể sửa thông tin muộn nhất trước ngày mùng 5 hàng tháng!", 
					    			UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
					    }
					}
				}
			}
		}	 
	}
}
